<?php


namespace app\api\controller\user;


use app\api\controller\Controller;
use app\api\model\UserCcard;

class Ccard extends Controller
{
    /* @var \app\api\model\User $user */
    private $user;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->user = $this->getUser();   // 用户信息
    }

    /**
     * 绑定消费银行卡
     */
    public function bindcard(){
        $prama = $this->request->post();
        $prama['user_id'] = ($this->user)->user_id;
        $ucmodel = new UserCcard();
        if (!$ucmodel->bindcard($prama)){
            return $this->renderError('绑定银行卡数据修改失败:'.$ucmodel->getError());
        }else{
            return $this->renderSuccess(null,'绑定银行卡成功!');
        }
    }

    /**
     * 解绑消费银行卡
     */
    public function unbindcard(){
        $prama = $this->request->post();
        $prama['user_id'] = ($this->user)->user_id;
        $ucmodel = new UserCcard();
        if (!$ucmodel->unbindcard($prama)){
            return $this->renderError('解绑银行卡数据修改失败:'.$ucmodel->getError());
        }else{
            return $this->renderSuccess(null,'解绑银行卡修改成功!');
        }
    }

    /**
     * 获取用户绑定信用卡
     * @return array
     */
    public function lists(){
        $ucmodel = new UserCcard();
        if (!$list = $ucmodel->getList(($this->user)->user_id)){
            return $this->renderError('用户未绑定或获取失败:'.$ucmodel->getError());
        }else{
            return $this->renderSuccess($list,'获取用户绑定信用卡成功!');
        }
    }

    /**
     * 获取用户绑定银行卡信息
     * @param  int $ccard_id  信用卡编号
     * @return array
     */
    public function details(){
        $ubmodel = new UserCcard();
        $prama = $this->request->post();
        if (!isset($prama['ccard_id'])){
            return $this->renderError('获取用户信用卡失败:缺少信用卡编号');
        }else{
            $ccard_id = $prama['ccard_id'];
        }
        if (!$list = $ubmodel->getcards($ccard_id,($this->user)->user_id)){
            return $this->renderError('获取用户信用卡失败:'.$ubmodel->getError());
        }else{
            return $this->renderSuccess($list,'获取信用卡成功!');
        }
    }
}